﻿using System.Collections.Generic;
using Kevin.Comm.Utility;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Products.Components.Category;
using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Service.Entity.StoreManagement.Enumerations;
using Kevin.Shop.Business.StoreManagement.Components;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Common.Helper;
using RiderShop.Mobile.ServiceEntity.Product;
using RiderShop.Mobile.ServiceEntity.Store;

namespace RiderShop.Mobile.Modle
{
    public class CategoryModel
    {
        /// <summary>
        /// 得到产品的一级分类
        /// </summary>
        /// <returns></returns>
        public void LoadAllSubcategory()
        {
            List<CategoryInfo> list = null;
            if (!KevinCache.Exists(CacheKeys.CacheKeyAllSubcategory))
            {
                list = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategories", (int)(CategoryLevelType.All)));
                KevinCache.Insert(CacheKeys.CacheKeyAllSubcategory, list, 60 * 60);
            }
            else
            {
                list = KevinCache.Get(CacheKeys.CacheKeyAllSubcategory) as List<CategoryInfo>;
            }

            if (list != null)
            {
                Groups = list.FindAll(p => (p.Depth == CategoryLevelType.Group));
                Categories = list.FindAll(p => (p.Depth == CategoryLevelType.Category));
                SubCategories = list.FindAll(p => (p.Depth == CategoryLevelType.SubCategory));
            }
        }

        /// <summary>
        /// 得到产品的一级分类
        /// </summary>
        /// <returns></returns>
        public void LoadAllSubcategoryExistProduct()
        {
            List<CategoryInfo> list = null;
            if (!KevinCache.Exists(CacheKeys.CacheKeyAllSubcategoryExistProduct))
            {
                list = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategoriesExistProduct", (int)(CategoryLevelType.All)));
                KevinCache.Insert(CacheKeys.CacheKeyAllSubcategoryExistProduct, list, 60 * 60);
            }
            else
            {
                list = KevinCache.Get(CacheKeys.CacheKeyAllSubcategoryExistProduct) as List<CategoryInfo>;
            }

            if (list != null)
            {
                SubCategories = list.FindAll(p => (p.Depth == CategoryLevelType.SubCategory));
                Categories = list.FindAll(p => (p.Depth == CategoryLevelType.Category && SubCategories.Exists(s => s.ParentCategoryId == p.CategoryId)));
                Groups = list.FindAll(p => (p.Depth == CategoryLevelType.Group && Categories.Exists(s => s.ParentCategoryId == p.CategoryId)));
            }
        }

        /// <summary>
        /// 得到产品的一级分类
        /// </summary>
        /// <returns></returns>
        public List<CategoryInfo> Groups { get; set; }

        /// <summary>
        /// 得到产品的二级分类
        /// </summary>
        /// <returns></returns>
        public List<CategoryInfo> Categories { get; set; }

        /// <summary>
        /// 得到产品的三级分类
        /// </summary>
        /// <returns></returns>
        public List<CategoryInfo> SubCategories { get; set; }

        /// <summary>
        /// 根据二级分类得到三级分类
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public List<CategoryInfo> GetCategoriesWithId(int categoryId)
        {
            List<CategoryInfo> categoryList = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategoriesWithId", categoryId));
            return categoryList;
        }

        /// <summary>
        /// 所有的三级分类JSON格式字符串
        /// </summary>
        public string AllSubCategoriesJSON { get; set; }

        public List<CategoryInfo> www(int categoryId)
        {
            List<CategoryInfo> categoryList = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategoriesWithId", categoryId));
            return categoryList;
        }
    }
}
